<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2018/11/28
 * Time: 13:49
 */

namespace App\Models;


use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class TaskLog extends Model
{
    protected $table = 'task_log';
    protected $guarded = [];
    public $timestamps = false;//关闭自动维护
    public $dates = ['created_at', 'updated_at'];

    /**
     * 更新任务记录表
     * @param int $uid
     * @param string $type
     * @param int $times
     */
    public function updateLog($uid = 0, $type = '',$times = 1,$param_id = 0)
    {
        $data['user_id'] = $uid;
        $data['created_at'] = Carbon::now();
        $data['task_type'] = $type;
        $data['param_id'] = $param_id;
        $data['score'] = 0;
        $data['info'] = config('task.info.'.$type);
        if($type == 'invite_friends'){
            if(config('task.maximum_times.invite_friends') >= $times){
                $data['score'] = '+'.config('task.score_rule.invite_friends.'.(string)($times-1));
            }
        }else{
            if(config('task.maximum_times.'.$type) >= $times){
                $data['score'] = '+'.config('task.score_rule.'.$type);
            }
        }

//        switch ($type) {
//            case 'watch':
//                $data['info'] = '观看视频';
//                if(config('task.maximum_times.watch') >= $times){
//                    $data['score'] = '+'.config('task.score_rule.watch');
//                }
//                break;
//            case 'shared_group':
//                $data['info'] = '分享到群';
//                if(config('task.maximum_times.shared_group') >= $times){
//                    $data['score'] = '+'.config('task.score_rule.shared_group');
//                }
//                break;
//            case 'shared_friends':
//                $data['info'] = '分享给朋友';
//                if(config('task.maximum_times.shared_friends') >= $times){
//                    $data['score'] = '+'.config('task.score_rule.shared_friends');
//                }
//                break;
//            case 'video_zans':
//                $data['info'] = '视频点赞';
//                if(config('task.maximum_times.video_zans') >= $times){
//                    $data['score'] = '+'.config('task.score_rule.video_zans');
//                }
//                break;
//            case 'video_collects':
//                $data['info'] = '视频收藏';
//                if(config('task.maximum_times.video_collects') >= $times){
//                    $data['score'] = '+'.config('task.score_rule.video_collects');
//                }
//                break;
//            case 'sign_in':
//                $data['info'] = '签到';
//                if(config('task.maximum_times.sign_in') >= $times){
//                    $data['score'] = '+'.config('task.score_rule.sign_in');
//                }
//                break;
//            case 'invite_friends':
//                if(config('task.maximum_times.invite_friends') >= $times){
//                    $data['info'] = '邀请新用户';
//                    $data['score'] = '+'.config('task.score_rule.invite_friends.'.(string)($times-1));
//                }
//                break;
//        }
        $this->create($data);
    }

    /**
     * 时间修饰器
     * @param $value
     * @return string
     */
    public function getCreatedAtAttribute($value)
    {
        $now = time();
        $time = Carbon::parse($value)->timestamp;
        $t = $now - $time;
        if ($t < 60) {
            $str = '刚刚';
        } elseif ($t < 60 * 60) {
            $min = floor($t / 60);
            $str = $min . '分钟前';
        } elseif ($t < 60 * 60 * 24) {
            $h = floor($t / (60 * 60));
            $str = $h . '小时前';
        } elseif ($t < 60 * 60 * 24 * 3) {
            $d = floor($t / (60 * 60 * 24));
            $str = $d . '天前';
        } else {
            $str = Carbon::parse($value)->format('Y-m-d');
        }
        return $str;
    }
}